// pages/my/modifymobile/modifymobile.js
let utils = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile:"",
    buttonDisable:false,
    verifyCodeTime:'发送验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 发送验证码
   */
  bindSendCode: function () {
    var that = this;
    var mobile = that.data.mobile;
    if (mobile == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
      return;
    }
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none'
      })
      return;
    }

    wx.request({
      url: utils.apiUrl + '/Shop/CheckModfiyMobileRepeat',
      data: {
        mobile: mobile
      },
      success: function (response) {
        var data = response.data;
        if (data.status == 0) {
          that.verifyCodeEvent();
          wx.request({
            url: utils.apiUrl + '/Common/SendVerifyCode',
            data: {
              mobile: mobile
            },
            success: function (response) {
              var data = response.data;
              if (data.status == 0) {
                wx.showToast({
                  title: '验证码发送成功',
                  icon: 'success'
                });
              } else {
                wx.showToast({
                  title: data.message,
                  icon: 'none'
                });
              }
            }
          });
        } else {
          wx.showToast({
            title: data.message,
            icon: 'none'
          });
        }
      }
    });

   

    
  },
  bindMobileInput: function (e) {
    this.setData({
      mobile: e.detail.value
    });
  },
  bindFormSubmit:function(e){
    var that = this;
    console.log(e);
    var formData = e.detail.value;
    if (formData.VerifyCode == '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    wx.showLoading({
      title: '正在提交,请等待..',
    })
    utils.postData(
      utils.apiUrl + '/shop/ModfiyMobile',
      formData,
      function (response) {
        if (response.status == 0) {
          wx.navigateTo({
            url: '../shopedit/shopedit?shopid=' + response.data,
          })
        }
        else {
          wx.showModal({
            title: '操作失败',
            content: response.message,
            showCancel: false,
          });
        }
        wx.hideLoading();
      }
    )
  },
  verifyCodeEvent: function () {
    var that = this;
    if (this.data.buttonDisable) return;
    
    var c = 60;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '发送验证码',
          buttonDisable: false
        })
      }
    }, 1000)
  }
})